package com.cn.springcloudwechat.serviceImpl.find.DelicateAskAnswer;

import com.cn.springcloudwechat.common.mybatis.Page;
import com.cn.springcloudwechat.common.utils.ErrorUtil;
import com.cn.springcloudwechat.mapper.mapper.find.DelicateAskAnswer.YmDelicateArticleMapper;
import com.cn.springcloudwechat.mapper.mapper.find.DelicateAskAnswer.YmDelicateAskAnswerMapper;
import com.cn.springcloudwechat.mapper.mapper.find.DelicateAskAnswer.YmDelicateMenuToArticleMapper;
import com.cn.springcloudwechat.model.find.DelicateAskAnswer.YmDelicateArticle;
import com.cn.springcloudwechat.model.find.DelicateAskAnswer.YmDelicateAskAnswer;
import com.cn.springcloudwechat.model.find.DelicateAskAnswer.YmDelicateMenuToArticle;
import com.cn.springcloudwechat.service.find.DelicateAskAnswer.DelicateAskAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.lang.reflect.Array;
import java.util.*;

@Service("DelicateAskAnswerService")
public class DelicateAskAnswerServiceImpl implements DelicateAskAnswerService {
    @Autowired
    private YmDelicateMenuToArticleMapper ymDelicateMenuToArticleMapper;

    @Autowired
    private YmDelicateArticleMapper ymDelicateArticleMapper;

    @Autowired
    private YmDelicateAskAnswerMapper ymDelicateAskAnswerMapper;


    private Integer i = 0;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addMenu(YmDelicateAskAnswer ymDelicateAskAnswer) {
        String name=ymDelicateAskAnswer.getDelicateMenuName();
        ymDelicateAskAnswer.setCreateTime(new Date());
        if(ymDelicateAskAnswer.getDelicateMenuIco()==null){
        if(ymDelicateAskAnswer.getMenuGrade()==1) {
            ymDelicateAskAnswer.setDelicateMenuIco("ask1.png");
        }
        if(ymDelicateAskAnswer.getMenuGrade()==2){
            String[] icon={"children.png","zu44.png","zhishi.png","naiping.png","shengzhang.png","yiyaoxiang.png"};
            Random random=new Random(47);
            ymDelicateAskAnswer.setDelicateMenuIco(icon[random.nextInt(6)]);
        }
        }
        Assert.notNull(name,String.format(ErrorUtil.PARAMETER_NULL,"MenuName"));
        i=ymDelicateAskAnswerMapper.insert(ymDelicateAskAnswer);
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteMenu(Long[] ymDelicateMenuIds,int state) {
        for(Long ymDelicateMenuId:ymDelicateMenuIds) {
            Assert.notNull(ymDelicateMenuId, String.format(ErrorUtil.PARAMETER_NULL, "menuId"));
            Assert.notNull(state, String.format(ErrorUtil.PARAMETER_NULL, "state"));
            YmDelicateAskAnswer ymDelicateAskAnswer = ymDelicateAskAnswerMapper.selectByPrimaryKey(ymDelicateMenuId, null);
            if (ymDelicateAskAnswer.getDelicateParentMenuId() == null || ymDelicateAskAnswer.getDelicateParentMenuId().equals("")) {
                //一级菜单
                ArrayList<Long> MenuId = new ArrayList<Long>();
                ArrayList<Long> childId_two = ymDelicateAskAnswerMapper.selectChildId(ymDelicateMenuId, null);
                MenuId.add(ymDelicateMenuId);
                MenuId.addAll(childId_two);
                ArrayList<Long> childId_Three = new ArrayList<Long>();
                for (Long m : childId_two) {
                    childId_Three.addAll(ymDelicateAskAnswerMapper.selectChildId(m, null));

                }
                MenuId.addAll(childId_Three);
                ArrayList<Long> articleId = new ArrayList<Long>();
                for (Long n : childId_Three) {
                    articleId.addAll(ymDelicateMenuToArticleMapper.selectArticleId(n, null));
                }
                ymDelicateMenuToArticleMapper.deleteByArticleId(articleId, state);
                ymDelicateArticleMapper.deleteByPrimaryKey(articleId, state);
                i = ymDelicateAskAnswerMapper.deleteByPrimaryKey(MenuId, state);
            } else if (ymDelicateAskAnswer.getDelicateMenuIco() == null || ymDelicateAskAnswer.getDelicateMenuIco().length() == 0) {
                ArrayList<Long> articleId = ymDelicateMenuToArticleMapper.selectArticleId(ymDelicateMenuId, null);
                ymDelicateMenuToArticleMapper.deleteByArticleId(articleId, state);
                ymDelicateArticleMapper.deleteByPrimaryKey(articleId, state);
                ArrayList<Long> MenuId = new ArrayList<Long>();
                MenuId.add(ymDelicateMenuId);
                 i = ymDelicateAskAnswerMapper.deleteByPrimaryKey(MenuId, state);

                //三级菜单
            } else {
                //二级菜单
                ArrayList<Long> childId_Three = ymDelicateAskAnswerMapper.selectChildId(ymDelicateMenuId, null);
                ArrayList<Long> MenuId = new ArrayList<Long>();
                MenuId.add(ymDelicateMenuId);
                MenuId.addAll(childId_Three);
                ArrayList<Long> articleId = new ArrayList<Long>();
                for (Long n : childId_Three) {
                    articleId.addAll(ymDelicateMenuToArticleMapper.selectArticleId(n, null));

                }
                ymDelicateMenuToArticleMapper.deleteByArticleId(articleId, state);
                ymDelicateArticleMapper.deleteByPrimaryKey(articleId, state);

                 i = ymDelicateAskAnswerMapper.deleteByPrimaryKey(MenuId, state);
            }

        }
    return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public YmDelicateAskAnswer selectMenu(Long ymDelicateMenuId) {
        Assert.notNull(ymDelicateMenuId,String.format(ErrorUtil.PARAMETER_NULL,"menuId"));
        YmDelicateAskAnswer ymDelicateAskAnswer=ymDelicateAskAnswerMapper.selectByPrimaryKey(ymDelicateMenuId,null);
        return ymDelicateAskAnswer;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ArrayList<YmDelicateAskAnswer> selectParentMenu() {
        return ymDelicateAskAnswerMapper.selectParentMenu();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ArrayList<YmDelicateAskAnswer> selectChildMenu(){

        return  ymDelicateAskAnswerMapper.selectChildMenu();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addArticle(Long ymDelicateMenuId,YmDelicateArticle ymDelicateArticle) {
        Assert.notNull(ymDelicateMenuId,String.format(ErrorUtil.PARAMETER_NULL,"menuId"));
        ymDelicateArticle.setCreateTime(new Date());
        i=ymDelicateArticleMapper.insert(ymDelicateArticle);
        //配置菜单文章关联信息
        YmDelicateMenuToArticle ymDelicateMenuToArticle=new YmDelicateMenuToArticle();
        ymDelicateMenuToArticle.setDelicateArticleId(ymDelicateArticle.getDelicateArticleId());
        ymDelicateMenuToArticle.setDelicateMenuId(ymDelicateMenuId);
        ymDelicateMenuToArticle.setCreateTime(new Date());
        Integer j=ymDelicateMenuToArticleMapper.insert(ymDelicateMenuToArticle);

        //两个操作都成功才算成功
        if(i+j==2){
            return 1;
        }
        else return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteArticle(ArrayList<Long> ymDelicateArticleId,int state) {
        Assert.notNull(ymDelicateArticleId,String.format(ErrorUtil.PARAMETER_NULL,"articleId"));
        i=ymDelicateArticleMapper.deleteByPrimaryKey(ymDelicateArticleId,state);
        ymDelicateMenuToArticleMapper.deleteByArticleId(ymDelicateArticleId,state);
        return i;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public YmDelicateArticle selectArticle(Long ymDelicateArticleId) {
        Assert.notNull(ymDelicateArticleId,String.format(ErrorUtil.PARAMETER_NULL,"articleId"));
        return ymDelicateArticleMapper.selectByPrimaryKey(ymDelicateArticleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ArrayList<YmDelicateArticle> selectArticleList(Long ymDelicateMenuId) {
        Assert.notNull(ymDelicateMenuId,String.format(ErrorUtil.PARAMETER_NULL,"MenuId"));
        return ymDelicateArticleMapper.selectArticleByMenuId(ymDelicateMenuId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ArrayList<YmDelicateAskAnswer> selectThreeMenuByParentId(Long ymDelicateParentId) {
        Assert.notNull(ymDelicateParentId,String.format(ErrorUtil.PARAMETER_NULL,"MenuId"));
        ArrayList<Long>childId_two=ymDelicateAskAnswerMapper.selectChildId(ymDelicateParentId,1);
        ArrayList<Long>childId_three=new ArrayList<Long>();
        for(Long m:childId_two){
            childId_three.addAll(ymDelicateAskAnswerMapper.selectChildId(m,1));
        }
        ArrayList<YmDelicateAskAnswer> MenuList=ymDelicateAskAnswerMapper.selectMenuList(childId_three,1);
        return MenuList;
    }

    /**
     * 后台查询菜单
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page<YmDelicateAskAnswer> queryMenuAdmin(YmDelicateAskAnswer ymDelicateAskAnswer,Page<YmDelicateAskAnswer> page) {

        ArrayList<YmDelicateAskAnswer> ymDelicateAskAnswers= ymDelicateAskAnswerMapper.queryMenuAdmin(ymDelicateAskAnswer, page);
        for(YmDelicateAskAnswer menu:ymDelicateAskAnswers){
           menu.setNodes(ymDelicateAskAnswerMapper.queryChildMenuByParentId(menu.getDelicateMenuId()));
           for(YmDelicateAskAnswer child:menu.getNodes()){
               child.setNodes(ymDelicateAskAnswerMapper.queryChildMenuByParentId(child.getDelicateMenuId()));
           }
        }
        page.setResults(ymDelicateAskAnswers);
        return page;
    }

    /**
     * 后台查询文章
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page<HashMap<String,Object>> queryArticleAdmin( YmDelicateArticle ymDelicateArticle,Page<HashMap<String,Object>> page) {
        ArrayList<YmDelicateArticle>ymDelicateArticles= ymDelicateArticleMapper.queryArticleAdmin(ymDelicateArticle,page);
        ArrayList<Long>articleIds=new ArrayList<Long>();
        for(YmDelicateArticle article:ymDelicateArticles)
            articleIds.add(article.getDelicateArticleId());
        ArrayList<Long> menuIds=ymDelicateMenuToArticleMapper.selectMenuId(articleIds,null);
        ArrayList<YmDelicateAskAnswer> ymDelicateAskAnswers=new ArrayList<YmDelicateAskAnswer>();
        for(Long menuId:menuIds){
            ymDelicateAskAnswers.add(ymDelicateAskAnswerMapper.selectByPrimaryKey(menuId,null));
        }
        ArrayList<HashMap<String,Object>> result=new ArrayList<HashMap<String,Object>>();
        for(int i=0;i<ymDelicateArticles.size();i++){
            HashMap<String,Object> map=new HashMap<String,Object>();
            map.put("ymDelicateArticle",ymDelicateArticles.get(i));
            map.put("ymDelicateAskAnswer",ymDelicateAskAnswers.get(i));
            System.out.println(map);
            result.add(map);
        }
        System.out.println(result);
        page.setResults(result);
        return page;
    }

    /**
     * 通过父级菜单id查子级菜单
     *
     * @param parentId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ArrayList<YmDelicateAskAnswer> queryChildMenuByParentId(Long parentId) {
        Assert.notNull(parentId,String.format(ErrorUtil.PARAMETER_NULL,"parentId"));
        return ymDelicateAskAnswerMapper.queryChildMenuByParentId(parentId);
    }

    /**
     * 查询父菜单菜单
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ArrayList<YmDelicateAskAnswer> queryParentMenuAdmin(int menuGrade) {
        if(menuGrade==3)
          return ymDelicateAskAnswerMapper.selectThreeMenuList();
        if(menuGrade==2)
            return ymDelicateAskAnswerMapper.selectChildMenu();
        if(menuGrade==1)
            return ymDelicateAskAnswerMapper.selectParentMenu();
        return null;
    }

    /**
     * 更改文章
     *
     * @param ymDelicateArticle
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateArticle(YmDelicateArticle ymDelicateArticle,Long ymDelicateMenuId) {
        ymDelicateArticle.setUpdateTime(new Date());
        Assert.notNull(ymDelicateArticle.getDelicateArticleId(),String.format(ErrorUtil.PARAMETER_NULL,"ArticleId"));
        if(ymDelicateMenuId!=null&&!ymDelicateMenuId.equals("")){
            ymDelicateMenuToArticleMapper.updateMenuId(ymDelicateMenuId,ymDelicateArticle.getDelicateArticleId());
        }
        return ymDelicateArticleMapper.updateArticle(ymDelicateArticle);
    }

    /**
     * 更改菜单
     *
     * @param ymDelicateAskAnswer
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateMenu(YmDelicateAskAnswer ymDelicateAskAnswer) {
        ymDelicateAskAnswer.setUpdateTime(new Date());
        Assert.notNull(ymDelicateAskAnswer.getDelicateMenuId(),String.format(ErrorUtil.PARAMETER_NULL,"MenuId"));

        return ymDelicateAskAnswerMapper.updateMenu(ymDelicateAskAnswer);
    }


}